﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using ICeFramework.Common.DataLayer;

namespace ICeFramework.Business.DataLayer
{
    public class EntityRowCollection<TEntity, TEntityRow> : ICollection<TEntityRow>
        where TEntity : class
        where TEntityRow : class
    {
        private List<TEntityRow> _list = new List<TEntityRow>();
        private EntityBase<TEntity, TEntityRow> _entity;

        #region public TEntityRow this[int rowIndex]
        /// <summary>
        /// Gets the <see cref="TEntityRow"/> item identified by the given arguments of the EntityRowCollection
        /// </summary>
        /// <value></value>
        public TEntityRow this[int rowIndex]
        {
            get { return _list[rowIndex]; }
        }
        #endregion

        #region public IEntityObject this[int rowIndex, int columnIndex]
        /// <summary>
        /// Gets the <see cref="IEntityObject"/> item identified by the given arguments of the EntityRowCollection
        /// </summary>
        /// <value></value>
        public IEntityObject this[int rowIndex, int columnIndex]
        {
            get { return (_list[rowIndex] as EntityRowBase<TEntity, TEntityRow>)[columnIndex]; }
        }
        #endregion

        #region public IEntityObject this[int rowIndex, EntityColumn column]
        /// <summary>
        /// Gets the <see cref="IEntityObject"/> item identified by the given arguments of the EntityRowCollection
        /// </summary>
        /// <value></value>
        public IEntityObject this[int rowIndex, EntityColumn column]
        {
            get { return (_list[rowIndex] as EntityRowBase<TEntity, TEntityRow>)[column]; }
        }
        #endregion

        #region public EntityRowCollection(EntityBase<TEntity, TEntityRow> entity)
        /// <summary>
        /// Initializes a new instance of the <b>EntityRowCollection&lt;TEntity, TEntityRow&gt;</b> class.
        /// </summary>
        /// <param name="entity"></param>
        public EntityRowCollection(EntityBase<TEntity, TEntityRow> entity)
        {
            _entity = entity;
        }
        #endregion

        #region public TEntityRow Add()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public TEntityRow Add()
        {
            TEntityRow item = _entity.NewRow();
            _list.Add(item);
            return item;
        }

        #region public void Add(TEntityRow item)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        //[Obsolete("This method is not supported. Use <b>void TEntityRow Add()</b> instead", true)]
        public void Add(TEntityRow item)
        {
            _list.Add(item);
        }
        #endregion

        #region public void Clear(bool deleteDataTableRows)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="deleteDataTableRows"></param>
        public void Clear(bool deleteDataTableRows)
        {
            _list.Clear();
            if (deleteDataTableRows)
            {
                _entity.DataTable.Rows.Clear();
            }
        }
        #endregion

        #region public void Clear()
        /// <summary>
        /// 
        /// </summary>
        public void Clear()
        {
            Clear(false);
        }
        #endregion

        #region public bool Contains(TEntityRow item)
        /// <summary>
        /// Returns a value indicating whether the specified <see cref="TEntityRow"/>
        ///  is contained in the <see cref="Business.Common.EntityRowCollection{TEntity, TEntityRow}"/>.
        /// </summary>
        /// <param name="item">The <see cref="TEntityRow"/> to locate in the 
        /// <see cref="Business.Common.EntityRowCollection{TEntity, TEntityRow}"/>.</param>
        /// <returns><b>true</b> if the <i>TEntityRow</i> parameter is a member 
        /// of the <see cref="Business.Common.EntityRowCollection{TEntity, TEntityRow}"/>; otherwise, <b>false</b>.</returns>
        public bool Contains(TEntityRow item)
        {
            return _list.Contains(item);
        }
        #endregion

        #region public void CopyTo(TEntityRow[] array, int arrayIndex)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="arrayIndex"></param>
        public void CopyTo(TEntityRow[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }
        #endregion

        #region public int Count
        /// <summary>
        /// Gets the Count of the EntityRowCollection
        /// </summary>
        /// <value></value>
        public int Count
        {
            get { return _list.Count; }
        }
        #endregion

        #region public bool IsReadOnly
        /// <summary>
        /// Gets the IsReadOnly of the EntityRowCollection
        /// </summary>
        /// <value></value>
        public bool IsReadOnly
        {
            get { return _entity.DataTable.Rows.IsReadOnly; }
        }
        #endregion

        #region public bool Remove(TEntityRow item)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(TEntityRow item)
        {
            _entity.RemoveDataTableRow(item);
            return _list.Remove(item);
        }
        #endregion
        #endregion

        #region IEnumerable<TEntityRow> Members

        public IEnumerator<TEntityRow> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

    }
}
